package com.frogs.ucenter.v1.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.frogs.framework.base.service.daoimpl.BaseServiceImpl;
import com.frogs.framework.util.PageList;
import com.frogs.framework.util.date.DateUtil;
import com.frogs.system.util.HealthRecordStatusEnum;
import com.frogs.system.web.api.request.Request;
import com.frogs.system.web.api.response.Response;
import com.frogs.system.web.exception.ServiceOperationException;
import com.frogs.ucenter.dao.MedicalHistoryDao;
import com.frogs.ucenter.entity.MedicalHistory;
import com.frogs.ucenter.v1.request.MedicalHistoryDeleteRequest;
import com.frogs.ucenter.v1.request.MedicalHistoryListRequest;
import com.frogs.ucenter.v1.request.MedicalHistoryMergeRequest;
import com.frogs.ucenter.v1.response.AddResponse;
import com.frogs.ucenter.v1.response.ListResponse;
import com.frogs.ucenter.v1.service.MedicalHistoryService;
import com.frogs.ucenter.v1.util.ApiConstants;

@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
@Service(ApiConstants.VERSION + "medicalHistoryService")
public class MedicalHistoryServiceImpl extends BaseServiceImpl<MedicalHistory> implements MedicalHistoryService{

	private static final Logger log = LoggerFactory.getLogger(MedicalHistoryServiceImpl.class);

	@Resource
	private MedicalHistoryDao medicalHistoryDao;
	
	@Override
	public Response mergeRecord(Request request) throws Exception{
		AddResponse<MedicalHistory> response = new AddResponse<MedicalHistory>();
		try {
			MedicalHistoryMergeRequest mhMergeRequest = (MedicalHistoryMergeRequest)request;
			MedicalHistory medicalHistory = new MedicalHistory();
			Integer id = mhMergeRequest.getId();
			medicalHistory.setUserId(mhMergeRequest.getUserId());
			medicalHistory.setImgUrl(mhMergeRequest.getImgUrl());
			medicalHistory.setMedicalDetail(mhMergeRequest.getMedicalDetail());
			medicalHistory.setCheckTime(DateUtil.parseDate(mhMergeRequest.getCheckTime(),"yyyy-MM-dd HH:mm:ss"));
			medicalHistory.setCreateTime(new Date());
			medicalHistory.setStatus(mhMergeRequest.getStatus());
			if(id == 0){
				medicalHistoryDao.insert(medicalHistory);
				response.setInfo(medicalHistory);
				response.setTip("病历管理档案添加成功");
			}else{
				medicalHistory.setId(mhMergeRequest.getId());
				medicalHistoryDao.update(medicalHistory);
				response.setInfo(medicalHistory);
				response.setTip("病历管理档案修改成功");
			}
		} catch (Exception e) {
			log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
		return response;
	}

	@Override
	public Response listRecord(Request request) throws Exception{
		ListResponse<MedicalHistory> response = new ListResponse<MedicalHistory>();
		try {
			MedicalHistoryListRequest mhListRequest = (MedicalHistoryListRequest)request;
			Integer userId = mhListRequest.getUserId();
			Integer pageNo = mhListRequest.getPageNo();
			Integer pageSize = mhListRequest.getPageSize();
			Integer status = mhListRequest.getStatus();
			
			Map<String,Object> params = new HashMap<String,Object>();
			params.put("eqUserId", userId);
			params.put("eqStatus", status);
			params.put("orderByClause", "create_time desc");
			PageList<MedicalHistory> list = new PageList<MedicalHistory>();
			list = medicalHistoryDao.selectByMap(params, pageNo, pageSize);
			response.setList(list);
			response.setTip("病历管理档案查看成功");
			
		} catch (Exception e) {
			log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}

		return response;
	}

	/**
	 * 删除病历管理档案(逻辑删除)
	 */
	@Override
	public Response deleteRecord(Request request) throws Exception{
		AddResponse<MedicalHistory> response = new AddResponse<MedicalHistory>();
		try {
			MedicalHistoryDeleteRequest mhDeleteRequest = (MedicalHistoryDeleteRequest)request;
			MedicalHistory medicalHistory = new MedicalHistory();
			Integer id = mhDeleteRequest.getId();
			medicalHistory.setUserId(mhDeleteRequest.getUserId());
			medicalHistory.setImgUrl(mhDeleteRequest.getImgUrl());
			medicalHistory.setMedicalDetail(mhDeleteRequest.getMedicalDetail());
			medicalHistory.setCheckTime(DateUtil.parseDate(mhDeleteRequest.getCheckTime(),"yyyy-MM-dd HH:mm:ss"));
			medicalHistory.setCreateTime(new Date());
			medicalHistory.setStatus(HealthRecordStatusEnum.NO.getId());
			medicalHistory.setId(id);
			medicalHistoryDao.update(medicalHistory);
			response.setInfo(medicalHistory);
			response.setTip("病历管理档案删除成功");
			
		} catch (Exception e) {
			log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
		return response;
	}

}
